<template>
  <el-menu
    :default-active="activeMenu"
    mode="horizontal"
    @select="handleSelect"
  >
    <template v-for="(item, index) in topMenus">
      <el-menu-item
        :style="{ '--theme': theme }"
        :index="item.path"
        :key="index"
        v-if="index < visibleNumber"
        ><svg-icon :icon-class="item.meta.icon" style="fill: #ffffff" />
        <div class="top-menu-title">{{ item.meta.title }}</div>
      </el-menu-item>
    </template>

    <!-- 顶部菜单超出数量折叠 -->
    <el-submenu
      :style="{ '--theme': theme }"
      index="more"
      v-if="topMenus.length > visibleNumber"
    >
      <template slot="title">更多菜单</template>
      <template v-for="(item, index) in topMenus">
        <el-menu-item
          :index="item.path"
          :key="index"
          v-if="index >= visibleNumber"
          ><svg-icon :icon-class="item.meta.icon" style="color: #ffffff" />
          {{ item.meta.title }}</el-menu-item
        >
      </template>
    </el-submenu>
  </el-menu>
</template>

<script>
import { constantRoutes } from "@/router";

// 隐藏侧边栏路由
const hideList = ["/index", "/user/profile"];

export default {
  data() {
    return {
      // 顶部栏初始数
      visibleNumber: 6,
      // 当前激活菜单的 index
      currentIndex: undefined,
    };
  },
  computed: {
    theme() {
      return this.$store.state.settings.theme;
    },
    // 顶部显示菜单
    topMenus() {
      let topMenus = [];
      this.routers.map((menu) => {
        if (menu.hidden !== true) {
          // 兼容顶部栏一级菜单内部跳转
          if (menu.path === "/") {
            topMenus.push(menu.children[0]);
          } else {
            topMenus.push(menu);
          }
        }
      });
      return topMenus;
    },
    // 所有的路由信息
    routers() {
      return this.$store.state.permission.topbarRouters;
    },
    // 设置子路由
    childrenMenus() {
      var childrenMenus = [];
      this.routers.map((router) => {
        // console.log(router, '子路由信息11111111')
        for (var item in router.children) {
          if (router.children[item].parentPath === undefined) {
            // console.log('进入判断父路由确实存在--证明有子路由嵌套');
            if (router.path === "/") {
              router.children[item].path = "/" + router.children[item].path;
            } else {
              if (!this.ishttp(router.children[item].path)) {
                router.children[item].path =
                  router.path + "/" + router.children[item].path;
              }
            }
            router.children[item].parentPath = router.path;
          }
          childrenMenus.push(router.children[item]);
        }
      });
      return constantRoutes.concat(childrenMenus);
    },
    // 默认激活的菜单
    activeMenu() {
      const path = this.$route.path;
      let activePath = path;
      if (
        path !== undefined &&
        path.lastIndexOf("/") > 0 &&
        hideList.indexOf(path) === -1
      ) {
        const tmpPath = path.substring(1, path.length);
        activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
        if (!this.$route.meta.link) {
          this.$store.dispatch("app/toggleSideBarHide", false);
        }
      } else if (!this.$route.children) {
        activePath = path;
        this.$store.dispatch("app/toggleSideBarHide", true);
      }
      this.activeRoutes(activePath);
      return activePath;
    },
  },
  beforeMount() {
    window.addEventListener("resize", this.setVisibleNumber, { passive: true });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.setVisibleNumber);
  },
  mounted() {
    // this.setVisibleNumber();
  },
  methods: {
    // 根据宽度计算设置显示栏数
    setVisibleNumber() {
      debugger;
      const width = document.body.getBoundingClientRect().width / 3;
      this.visibleNumber = parseInt(width / 85);
    },
    // 菜单选择事件
    handleSelect(key, keyPath) {
      this.currentIndex = key;
      // console.log(key, keyPath, '为啥顶部选择的不起作用')
      const route = this.routers.find((item) => item.path === key);
      if (this.ishttp(key)) {
        // http(s):// 路径新窗口打开
        window.open(key, "_blank");
      } else if (!route || !route.children || key == "/goOut/index") {
        // console.log('首页和尾页', key)
        // 没有子路由路径内部打开
        this.$router.push({ path: key });
        this.$store.dispatch("app/toggleSideBarHide", true);
      } else {
        this.activeRoutes(key);
        this.hasChildren(key);
        this.$store.dispatch("app/toggleSideBarHide", false);
      }
    },
    // 当前激活的路由
    activeRoutes(key) {
      var routes = [];
      if (this.childrenMenus && this.childrenMenus.length > 0) {
        this.childrenMenus.map((item) => {
          if (key == item.parentPath || (key == "index" && "" == item.path)) {
            routes.push(item);
          }
        });
        this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
      } else {
        this.$store.dispatch("app/toggleSideBarHide", true);
      }
    },
    ishttp(url) {
      return url.indexOf("http://") !== -1 || url.indexOf("https://") !== -1;
    },
    hasChildren(key) {
      var routes = [];
      if (this.childrenMenus && this.childrenMenus.length > 0) {
        this.childrenMenus.map((item) => {
          if (key == item.parentPath || (key == "index" && "" == item.path)) {
            routes.push(item);
          }
        });
        this.activeChildren(routes[0]);
      }
    },
    activeChildren(routes) {
      if (routes.children && routes.children.length > 0) {
        this.activeChildren(routes.children[0]);
      } else {
        this.$router.push({
          name: routes.name,
        });
      }
    },
  },
};
</script>

<style lang="scss">
@import "~@/assets/styles/variables.scss";
$active-color: linear-gradient(
  0deg,
  rgba(24, 144, 255, 0.5) 0%,
  rgba(24, 144, 255, 0) 100%
);
:deep {
  .el-menu--horizontal {
    top: calc(#{$base-navbar-height} + 3px) !important;
  }
}
.topmenu-container.el-menu--horizontal {
  border-color: rgba(0, 0, 0, 0);
}
.topmenu-container.el-menu--horizontal > .el-menu-item {
  float: left;
  height: #{$base-navbar-height} !important;
  line-height: #{$base-navbar-height} !important;
  color: #ffffff !important;
  padding: 0 20px !important;
  margin: 0px !important;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  .svg-icon {
    height: 22px;
    width: 22px;
  }
  .top-menu-title {
    text-align: center;
    height: calc(#{$base-navbar-height} / 2 - 8px);
    line-height: calc(#{$base-navbar-height} / 2 - 8px);
  }
}

/* submenu item */
.topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title {
  float: left;
  height: #{$base-navbar-height} !important;
  line-height: #{$base-navbar-height} !important;
  color: #ffffff !important;
  padding: 0 5px !important;
}
.topmenu-container.el-menu--horizontal > .el-menu-item.is-active {
  background: $active-color;
}
.topmenu-container.el-menu--horizontal > .el-menu-item:not(.is-disabled):hover,
.topmenu-container.el-menu--horizontal > .el-menu-item:not(.is-disabled):focus,
.topmenu-container.el-menu--horizontal > .el-submenu:not(.is-disabled):hover,
.topmenu-container.el-menu--horizontal > .el-submenu:not(.is-disabled):focus,
.topmenu-container.el-menu--horizontal > .el-submenu:hover,
.topmenu-container.el-menu--horizontal > .el-submenu:focus,
.topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title:hover,
.topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title:focus {
  background: $active-color;
}

// 更多菜单下拉箭头颜色 -自定义蓝色调
.topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title > i {
  color: #ffffff;
}

// 更多菜单下拉面板 -自定义蓝色调
.el-menu--horizontal {
  background: $base-sub-menu-background;
}
.el-menu--horizontal .el-menu {
  background: $base-sub-menu-background;
}
.el-menu--horizontal .el-menu .el-menu-item,
.el-menu--horizontal .el-menu .el-submenu__title {
  background-color: $base-sub-menu-background;
  color: #ffffff;
}
.el-menu--horizontal .el-menu .el-menu-item:not(.is-disabled):hover,
.el-menu--horizontal .el-menu .el-menu-item:not(.is-disabled):focus {
  background: linear-gradient(
    90deg,
    rgba(24, 144, 255, 0.5) 0%,
    rgba(24, 144, 255, 0) 100%
  );
  color: #ffffff;
}
.el-menu--horizontal .el-menu .el-menu-item.is-active,
.el-menu--horizontal .el-menu .el-submenu.is-active > .el-submenu__title {
  color: #ffffff;
  background: linear-gradient(
    90deg,
    rgba(24, 144, 255, 0.5) 0%,
    rgba(24, 144, 255, 0) 100%
  );
}
</style>
